<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>Audio/Video Example - Record Plugin for Video.js</title>

  <link href="../../node_modules/video.js/dist/video-js.min.css" rel="stylesheet">
  <link href="../../dist/css/videojs.record.css" rel="stylesheet">
  <link href="../assets/css/examples.css" rel="stylesheet">

  <script src="../../node_modules/video.js/dist/video.min.js"></script>
  <script src="../../node_modules/recordrtc/RecordRTC.js"></script>
  <script src="../../node_modules/webrtc-adapter/out/adapter.js"></script>

  <script src="../../dist/videojs.record.js"></script>

  <style>
  /* change player background color */
  #myVideo {
      background-color: #9ab87a;
  }
  </style>
</head>
<body>

<video id="myVideo" class="video-js vjs-default-skin"></video>

<script>
/* eslint-disable */
var options = {
    controls: true,
    bigPlayButton: false,
    width: 320,
    height: 240,
    fluid: false,
    plugins: {
        record: {
            audio: true,
            video: true,
            maxLength: 10,
            displayMilliseconds: false,
            debug: true
        }
    }
};

var player = videojs('myVideo', options, function() {
    // print version information at startup
    var msg = 'Using video.js ' + videojs.VERSION +
        ' with videojs-record ' + videojs.getPluginVersion('record') +
        ' and recordrtc ' + RecordRTC.version;
    videojs.log(msg);
});

// error handling
player.on('deviceError', function() {
    console.log('device error:', player.deviceErrorCode);
});

player.on('error', function(element, error) {
    console.error(error);
});

// user clicked the record button and started recording
player.on('startRecord', function() {
    console.log('started recording!');
});

// user completed recording and stream is available
player.on('finishRecord', function() {
    // the blob object contains the recorded data that
    // can be downloaded by the user, stored on server etc.
    console.log('finished recording:', player.recordedData);

    // upload recorded data
    upload(player.recordedData);
});

function upload(blob) {
    // this upload handler is served using webpack-dev-server for
    // this example, see build-config/fragments/dev.js
    var serverUrl = '/upload';
    var formData = new FormData();
    formData.append('file', blob, blob.name);

    console.log('upload recording ' + blob.name + ' to ' + serverUrl);

    // start upload
    fetch(serverUrl, {
        method: 'POST',
        body: formData
    }).then(
        success => console.log('upload recording complete.')
    ).catch(
        error => console.error('an upload error occurred!')
    );
}
</script>

</body>
</html>
